import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/model/parent_get_homeworks_model.dart';
import 'package:appflutterhomeschool/model/tab_model.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import '../my_app_view_model.dart';

///用户： ninetailedfox
///时间： 2020/7/5
///邮箱： 1037438704@qq.com
///功能： 作业任务 状态管理

class TaskViewModel extends ChangeNotifier {
  BuildContext me;
  bool openScreen = false;

  //列表
  List<ParentGetHomeworksModel> parentGetHomeworkList;

  //学生科目
  String subjectId = "";

  List<TabModel> listTab = [
    TabModel("全部", true),
    TabModel("进行中", false),
    TabModel("待批改", false),
    TabModel("已批改", false),
  ];
  bool identity = false;

  TaskViewModel(this.me);

  initView() {
    MyAppViewModel myAppViewModel = Provider.of<MyAppViewModel>(me);
    identity = myAppViewModel.identity;
    if (identity) {
//    再cls_mapping_id/后面加上cls_mapping_id，在status/
//    后面加上status( 1: 进行中; 2: 未批改; 3 :已批改)筛选查询；不筛选如果单纯的使用去除status/1/
      Dio2Utils.getInstance().get(
          subjectId == ""
              ? "${ApiService.teacherHomeworks}cls_mapping_id/${myAppViewModel.studentInformationModel.classMappingId}/"
              : "${ApiService.teacherHomeworks}cls_mapping_id/${myAppViewModel.studentInformationModel.classMappingId}/status/$subjectId",
          {}, (successCallBack) {
        parentGetHomeworkList = [];
        List listData = successCallBack["data"];
        if (listData == null) {
          return;
        }
        if (listData.length <= 0) {
          return;
        }
        listData.forEach((element) {
          parentGetHomeworkList.add(ParentGetHomeworksModel.fromJson(element));
        });
      }, (errorCallBack) {});
    } else {
      //头部筛选
      Dio2Utils.getInstance().post(ApiService.getSutdentSubjects, {
        "cls_id": myAppViewModel.studentInformationModel.classId,
        //类型：String  必有字段  备注：班级id
        "student_id": myAppViewModel.studentInformationModel.subjectId
        //类型：String  必有字段  备注：学生id
      }, (successCallBack) {
        listTab.clear();
        Map dataMap = successCallBack["data"];
        if (dataMap == null) {
          return;
        }
        List listSubjects = dataMap["subjects"];
        if (listSubjects.length > 0) {
          listSubjects.forEach((element) {
            listTab.add(TabModel.formjson(element));
          });
        }
        listTab.first.display = true;
        notifyListeners();
      }, (errorCallBack) {});

      Dio2Utils.getInstance().post(ApiService.parentGetHomeworks, {
        "cls_id": myAppViewModel.studentInformationModel.classId,
        //类型：String  必有字段  备注：班级id
        "student_id": myAppViewModel.studentInformationModel.subjectId,
        //类型：String  必有字段  备注：学生id
        "subject_id": subjectId
        //类型：String  可有字段  备注：科目id
      }, (successCallBack) {
        parentGetHomeworkList = [];
        Map dataMap = successCallBack["data"];
        if (dataMap == null) {
          return;
        }
        List listHomeworks = dataMap["homeworks"];
        if (listHomeworks.length > 0) {
          listHomeworks.forEach((element) {
            if (element != null) {
              parentGetHomeworkList
                  .add(ParentGetHomeworksModel.fromJson(element));
            }
          });
        }
        notifyListeners();
      }, (errorCallBack) {});
    }

    notifyListeners();
  }

//是否开启筛选
  onTapOpenScreen() {
    openScreen = !openScreen;
    if (openScreen) {
      if (identity) {
        subjectId = "";
      } else {
        subjectId = listTab.first.id;
      }
    } else {
      for (int i = 0; i < listTab.length; i++) {
        listTab[i].display = false;
      }
      listTab.first.display = true;
      if (identity) {
        subjectId = "";
      }
    }
    initView();
    notifyListeners();
  }

  tapModelOnTap(int index) {
    for (int i = 0; i < listTab.length; i++) {
      listTab[i].display = false;
    }
    listTab[index].display = true;
    if (identity) {
      subjectId = "$index";
    } else {
      subjectId = listTab[index].id;
    }
    initView();
    notifyListeners();
  }

  //跳转到作业详情
  operationDetailsOnTap(int index) {
    if (parentGetHomeworkList == null) {
      return;
    }
    Application().navigatePushToPage(me, Routes.operationDetailsPage,
        {"id": parentGetHomeworkList[index].id});
  }

  //跳转到作业详情
  correctingOnTap(int index) {
    if (parentGetHomeworkList == null) {
      return;
    }
    PromptDialogWidgets(me).commentDialog(
        dahuiOnTap: (CommondModel commondModel) {
      Dio2Utils.getInstance().post(
          ApiService.teacherRepulseHomework,
          {
            "student_homework_id": parentGetHomeworkList[index].id,
            //类型：String  必有字段  备注：学生作业id
            "check_score": commondModel.count,
            //类型：String  必有字段  备注：教师评分
            "check_description": commondModel?.msg ?? ""
            //类型：String  必有字段  备注：教师评价
          },
          (successCallBack) {
            Fluttertoast.showToast(msg: "打回成功");
            initView();
          }, (errorCallBack) {
      });
    }, sureOnTap: (CommondModel commondModel) {
      Dio2Utils.getInstance().post(ApiService.teacherCheckHomework, {
        "student_homework_id": parentGetHomeworkList[index].id,
        //类型：String  必有字段  备注：学生作业id
        "check_score": commondModel.count,
        //类型：String  必有字段  备注：教师评分
        "check_description": commondModel?.msg ?? ""
        //类型：String  必有字段  备注：教师评价
      }, (successCallBack) {
        Fluttertoast.showToast(msg: "批改完成");
        initView();
      }, (errorCallBack) {});
    });
//    Application()
//        .navigatePushToPage(me, Routes.jobListStatusPage, {"id":parentGetHomeworkList[index].id});
  }
}
